package com.example.messenger;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class MessengerDataSource {
	
	private SQLiteDatabase db;
	private MessengerSQLiteHelper dbHelper;
	
	//All columns in the Messenger Table
	private String[] allColumns = { 
			MessengerSQLiteHelper.COLUMN_ID,
			MessengerSQLiteHelper.COLUMN_USERNAME,
			MessengerSQLiteHelper.COLUMN_SENDER,
			MessengerSQLiteHelper.COLUMN_MESSAGE,
			MessengerSQLiteHelper.COLUMN_TIME
			};
	
	//All the columns in the users table
	private String[] allUsers = {
			MessengerSQLiteHelper.COLUMN_USER,
			MessengerSQLiteHelper.COLUMN_PASSWORD
	};
	
	/*
	 * Constructor
	 */
	public MessengerDataSource(Context context){
		dbHelper = new MessengerSQLiteHelper(context);
	}
	
	/*
	 * DB operations
	 */
	public void open() throws SQLException{
		db = dbHelper.getWritableDatabase();
	}
	
	public void close(){
		dbHelper.close();
	}
	
	//Query Functions
	
	//Update User info in DB
	public void updateUser(String username, String password) throws Exception{
		String USERNAME_UPDATE = "UPDATE users SET username= " + "'" + username + "'" + " WHERE uid = 1";
		String PASSWORD_UPDATE = "UPDATE users SET password= " + "'" + password + "'" + " WHERE uid = 1";
		
		db.execSQL(USERNAME_UPDATE);
		db.execSQL(PASSWORD_UPDATE);

	}
	
	//insert message to sqlite
	public void insertMessage(String username, String sender, String message, String datetime) throws Exception{
		String INSERT_MESSAGE = "INSERT INTO messages (musername,sender,message,time) VALUES ("+ "'" + username + "'," + "'" + sender + "',"+ "'" + message + "',"+ "'" + datetime + "')";
		
		db.execSQL(INSERT_MESSAGE);

	}
	
	public void logoutUser() throws Exception{
		String USERNAME_UPDATE = "UPDATE users SET username= 'user' WHERE uid = 1";
		String PASSWORD_UPDATE = "UPDATE users SET password= 'pass' WHERE uid = 1";
		
		db.execSQL(USERNAME_UPDATE);
		db.execSQL(PASSWORD_UPDATE);

	}
	
	public void deleteMessage(String sender,String time){
		String MESSAGE_DELETE = "DELETE FROM messages WHERE sender = " + "'" + sender + "'" + "AND time=" + "'" + time + "'";
		
		db.execSQL(MESSAGE_DELETE);
	}
	
	//Get User info
	
	
	public String getUsername(){
		Cursor c = db.query(MessengerSQLiteHelper.TABLE_USERS, allUsers, MessengerSQLiteHelper.COLUMN_USER_ID + " = 1", null, null, null, null);
		if(c.getCount() == 0){
			Log.d("CM", "Couldn't find user ");
			return null;
		}
		c.moveToFirst();
		return c.getString(0);
	}

	public Cursor getMessages(){
		return db.query(MessengerSQLiteHelper.TABLE_MESSAGES, allColumns, MessengerSQLiteHelper.COLUMN_USERNAME + " = '" + getUsername() + "'", null, null, null, null);
	}
	
	public void insertMessage(Messages message){
		String MESSAGE_INSERT = "INSERT INTO messages (" +
				MessengerSQLiteHelper.COLUMN_USERNAME + "," +
				MessengerSQLiteHelper.COLUMN_SENDER + "," +
				MessengerSQLiteHelper.COLUMN_MESSAGE + "," +
				MessengerSQLiteHelper.COLUMN_TIME + ") VALUES (" +
					message.getReciever() + "," +
					message.getSender() + "," +
					message.getMessage() + "," +
					message.getTime() + ")";
		
		db.execSQL(MESSAGE_INSERT);
	}
}
